package web

import (
	"errors"
	"gitee.com/ZLX0326/gin_learn/bingweb/internal/domain"
	"gitee.com/ZLX0326/gin_learn/bingweb/internal/service"
	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
	JWT "github.com/golang-jwt/jwt/v5"
	"net/http"
)

// 定义User相关的路由
type UserHandle struct {
	Svc *service.UserService
}

func (uh *UserHandle) RegisterRouter(s *gin.Engine) {
	sg := s.Group("/user")
	sg.GET("/profile", uh.Profile)
	sg.POST("/login", uh.LoginJWT)
	sg.POST("/signIn", uh.SignIn)
	sg.PUT("/edit", uh.Edit)
}
func (uh *UserHandle) Login(ctx *gin.Context) {
	type LoginRes struct {
		Email    string `json:"email"`
		Password string `json:"password"`
	}
	var req LoginRes
	err := ctx.Bind(&req)
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"message": "参数传递错误！",
			"data":    nil,
		})
		return
	}

	u, err := uh.Svc.Login(ctx, req.Email, req.Password)
	if errors.Is(err, service.PasswordFailedError) {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "用户名或密码粗错误",
			"data":    nil,
		})
		return
	}
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "系统错误",
			"data":    nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "登陆成功",
		"data":    nil,
	})
	// 存储session
	sess := sessions.Default(ctx)
	sess.Set("uid", u.Id)
	_ = sess.Save()

	return

}
func (uh *UserHandle) LoginJWT(ctx *gin.Context) {
	type LoginRes struct {
		Email    string `json:"email"`
		Password string `json:"password"`
	}
	var req LoginRes
	err := ctx.Bind(&req)
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"message": "参数传递错误！",
			"data":    nil,
		})
		return
	}

	u, err := uh.Svc.Login(ctx, req.Email, req.Password)
	if errors.Is(err, service.PasswordFailedError) {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "用户名或密码粗错误",
			"data":    nil,
		})
		return
	}
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "系统错误",
			"data":    nil,
		})
		return
	}

	// 生成JWT
	token := JWT.New(JWT.SigningMethodHS256)
	token.Claims = &JWT.MapClaims{"uid": u.Id}
	tokenStr, err := token.SignedString([]byte("auth_sign"))
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "系统错误",
			"data":    nil,
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "登陆成功",
		"data":    tokenStr,
	})
	return

}
func (uh *UserHandle) SignIn(ctx *gin.Context) {
	type SignInReq struct {
		Email         string `json:"email"`
		Passwd        string `json:"passwd"`
		ConfirmPasswd string `json:"confirm_passwd"`
	}
	var req SignInReq
	if err := ctx.Bind(&req); err != nil {
		return
	}
	err := uh.Svc.SingUp(ctx, domain.User{
		Email:  req.Email,
		Passwd: req.Passwd,
	})
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"message": "注册失败",
			"code":    500,
			"data":    nil,
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"message": "注册成功",
		"code":    200,
		"data":    nil,
	})
	return
}
func (uh *UserHandle) Profile(ctx *gin.Context) {

}
func (uh *UserHandle) Edit(ctx *gin.Context) {

}
